interface CameraOption {
  name: string;
  password: string;
  IP: string;
  Port: string;
  width?: number;
  height?: number;
}
const WebVideoCtrl = (window as any).WebVideoCtrl;
export class Camera {
  DOMid = "divPlugin";
  szUsername = "";
  szPassword = "";
  szIP = "";
  szPort = "";
  szDeviceIdentify = "";
  width = 500;
  height = 500;
  iPrototocol = "http"; //'协议'
  iDevicePort = ""; //设备端口
  iRtspPort = ""; //RTSP端口
  iStreamType = 1; //码流类型 1-主码流，2-子码流，默认使用主码流预览
  bZeroChannel = false; //是否播放零通道
  g_iWndIndex = 0; //可以不用设置这个变量，有窗口参数的接口中，不用传值，开发包会默认使用当前选择窗口
  constructor({ name, password, IP, Port, width, height }: CameraOption) {
    this.szUsername = name;
    this.szPassword = password;
    this.szIP = IP;
    this.szPort = Port;
    this.szDeviceIdentify = `${IP}_${Port}`;
    if (width) this.width = width;
    if (height) this.height = height;
    this.init();
  }
  init = () => {
    //初始化
    WebVideoCtrl.I_InitPlugin(this.width, this.height, {
      bWndFull: true, //是否支持单窗口双击全屏，默认支持 true:支持 false:不支持
      iPackageType: 2,
      bNoPlugin: true,
      cbInitPluginComplete: () => {
        //成功回调
        WebVideoCtrl.I_InsertOBJECTPlugin("divPlugin");
        // 检查插件是否最新
        if (-1 == WebVideoCtrl.I_CheckPluginVersion()) {
          alert("检测到新的插件版本，双击开发包目录里的WebComponentsKit.exe升级！");
          return;
        }
      }
    });
  };
  login = (iPrototocol?: string) => {
    //登录
    if (iPrototocol) this.iPrototocol = iPrototocol;
    WebVideoCtrl.I_Login(this.szIP, this.iPrototocol, this.szPort, this.szUsername, this.szPassword, {
      success: (xmlDoc: any) => {
        console.log(xmlDoc);
        this.getDevicePort();
      },
      error: (status: any, xmlDoc: any) => {
        console.log("login", status, xmlDoc);
      }
    });
  };
  // 退出
  logout = () => {
    let iRet = WebVideoCtrl.I_Logout(this.szDeviceIdentify);
    if (iRet == 0) {
      console.log("退出成功");
    } else {
      console.log("退出失败");
    }
    return iRet;
  };
  // 获取端口
  getDevicePort = () => {
    let oPort = WebVideoCtrl.I_GetDevicePort(this.szDeviceIdentify);
    this.iDevicePort = oPort.iDevicePort;
    this.iRtspPort = oPort.iRtspPort;
  };
  // 获取通道
  getChannelInfo = (type: any = false) => {
    if (type == 1 || !type) this.GetAnalog();
    if (type == 2 || !type) this.GetDigital();
    if (type == 3 || !type) this.GetZero();
  };
  // 模拟通道
  GetAnalog = () => {
    WebVideoCtrl.I_GetAnalogChannelInfo(this.szDeviceIdentify, {
      async: false,
      success: (xmlDoc: any) => {
        console.log(xmlDoc);
        // let oChannels = $(xmlDoc).find("VideoInputChannel");
        //         $.each(oChannels, function (i) {
        //             let id = $(this).find("id").eq(0).text(),
        //                 name = $(this).find("name").eq(0).text();
        //             if ("" == name) {
        //                 name = "Camera " + (i < 9 ? "0" + (i + 1) : (i + 1));
        //             }
        //         });
      },
      error: (status: any, xmlDoc: any) => {
        console.log("GetAnalog", status, xmlDoc);
      }
    });
  };
  // 数字通道
  GetDigital = () => {
    WebVideoCtrl.I_GetDigitalChannelInfo(this.szDeviceIdentify, {
      async: false,
      success: (xmlDoc: any) => {
        console.log(xmlDoc);
        // let oChannels = $(xmlDoc).find("InputProxyChannelStatus");
        // $.each(oChannels, function (i) {
        //     let id = $(this).find("id").eq(0).text(),
        //         name = $(this).find("name").eq(0).text(),
        //         online = $(this).find("online").eq(0).text();
        //     if ("false" == online) {// 过滤禁用的数字通道
        //         return true;
        //     }
        //     if ("" == name) {
        //         name = "IPCamera " + (i < 9 ? "0" + (i + 1) : (i + 1));
        //     }
        //     oSel.append("<option value='" + id + "' bZero='false'>" + name + "</option>");
        // });
      },
      error: (status: any, xmlDoc: any) => {
        console.log("GetDigital", status, xmlDoc);
      }
    });
  };
  // 零通道
  GetZero = () => {
    WebVideoCtrl.I_GetZeroChannelInfo(this.szDeviceIdentify, {
      async: false,
      success: (xmlDoc: any) => {
        console.log(xmlDoc);
        // let oChannels = $(xmlDoc).find("ZeroVideoChannel");
        // $.each(oChannels, function (i) {
        //     let id = $(this).find("id").eq(0).text(),
        //         name = $(this).find("name").eq(0).text();
        //     if ("" == name) {
        //         name = "Zero Channel " + (i < 9 ? "0" + (i + 1) : (i + 1));
        //     }
        //     if ("true" == $(this).find("enabled").eq(0).text()) {// 过滤禁用的零通道
        //         oSel.append("<option value='" + id + "' bZero='true'>" + name + "</option>");
        //     }
        // });
      },
      error: (status: any, xmlDoc: any) => {
        console.log("GetZero", status, xmlDoc);
      }
    });
  };
  // 开始预览
  clickStartRealPlay = () => {
    let oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
    let startRealPlay = () => {
      WebVideoCtrl.I_StartRealPlay(this.szDeviceIdentify, {
        iRtspPort: this.iRtspPort, //RTSP 端口号，可以选择传入，如果不传，开发包会自动判断设备的 RTSP 端口
        iStreamType: this.iStreamType,
        iChannelID: 1, //播放通道号，默认通道 1
        bZeroChannel: this.bZeroChannel, //是否播放零通道，默认为 false
        success: () => {
          console.log("成功");
        },
        error: (status: any, xmlDoc: any) => {
          // status 403 设备不支持Websocket取流！
          console.log("GetZero", status, xmlDoc);
        }
      });
    };
    if (oWndInfo != null) {
      // 已经在播放了，先停止
      WebVideoCtrl.I_Stop({
        success: () => {
          startRealPlay();
        }
      });
    } else {
      startRealPlay();
    }
  };
  // 停止预览
  clickStopRealPlay = () => {
    let oWndInfo = WebVideoCtrl.I_GetWindowStatus(this.g_iWndIndex);
    if (oWndInfo != null) {
      WebVideoCtrl.I_Stop({
        success: () => {
          console.log("停止预览成功");
        },
        error: () => {
          console.log("停止预览失败");
        }
      });
    }
  };
}
